home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_075 / comm / util.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  12KB  |  538 lines

  1. /* Comm misc utilities */
  2. #define  UTIL  1
  3. #include "globals.h"
  4. #include "status.h"
  5.  
  6. extern struct Window *OpenWindow(), *MyOpenWindow();
  7. extern struct IntuiMessage *GetMsg();
  8. extern UBYTE  *AllocMem();
  9. extern int    MyCloseWindow();
  10. extern void   SetWindowTitles(), SizeWindow(), MoveWindow();
  11. void   _emit(), _emits();
  12.  
  13. #define REQOBPAIRS (sizeof(outerborder)/4) /* coord pairs in border below */
  14.  
  15. SHORT outerborder[] =
  16.    { /* x coord   y coord  */
  17.       2          , 10          , REQWIDTH+2 , 10          ,
  18.       REQWIDTH+2 , REQHEIGHT+11, 2          , REQHEIGHT+11,
  19.       2          , 11          , REQWIDTH+1 , 11          ,
  20.       REQWIDTH+1 , REQHEIGHT+10, 3          , REQHEIGHT+10,
  21.       3          , 11          , 4          , 11          ,
  22.       4          , REQHEIGHT+10, REQWIDTH   , REQHEIGHT+10,
  23.       REQWIDTH   , 12          , REQWIDTH-1 , 12          ,
  24.       REQWIDTH-1 , REQHEIGHT+10, 5          , REQHEIGHT+10,
  25.       5          , 12
  26.    };
  27.  
  28. #define REQIBPAIRS  (sizeof(innerborder)/4) /* coord pairs in border below */
  29.  
  30. SHORT innerborder[] =
  31.    {  /* x coord     y coord  */
  32.       7          , 13          , REQWIDTH-4 , 13          ,
  33.       REQWIDTH-4 , REQHEIGHT+8 , 7          , REQHEIGHT+8 ,
  34.       7          , 13          , 8          , 13          ,
  35.       8          , REQHEIGHT+8 , REQWIDTH-5 , REQHEIGHT+8 ,
  36.       REQWIDTH-5 , 13
  37.    };
  38.  
  39.  
  40. struct Border    Req_i_border =
  41.    {
  42.       0,0,ORANGE,ORANGE,JAM2,REQIBPAIRS,&innerborder[0],NULL
  43.    };
  44.  
  45. struct Border    Reqborder =
  46.    {
  47.       0,0,ORANGE,ORANGE,JAM2,REQOBPAIRS,&outerborder[0],&Req_i_border
  48.    };
  49.  
  50. struct IntuiText
  51.    No_File     = {BLACK, WHITE,JAM2,40,5,0,(UBYTE *)"  No such file!",NULL},
  52.    What_to_do  = {BLACK, WHITE,JAM2,40,5,0,(UBYTE *)"Delete existing file?",NULL},
  53.    File_exists = {BLACK, WHITE,JAM2,40,5,0,(UBYTE *)"File already exists!",NULL },
  54.    Append      = {BLACK, WHITE,JAM2, 6,3,0,(UBYTE *)"APPEND",NULL},
  55.    Delete      = {ORANGE,WHITE,JAM2, 6,3,0,(UBYTE *)"DELETE",NULL},
  56.    Continue    = {BLACK, WHITE,JAM2, 6,3,0,(UBYTE *)"CONTINUE",NULL},
  57.    Cancel      = {ORANGE,WHITE,JAM2, 6,3,0,(UBYTE *)" CANCEL ",NULL};
  58.  
  59. extern void emits_rx();
  60.  
  61. Show_Status()
  62. {
  63.   if(( st_window = MyOpenWindow(&STwindow,(ULONG)GADGETUP) ) == NULL)
  64.   {
  65.      emits_rx("Can't open window\n");
  66.      return FALSE;
  67.   }
  68.   SetMenuStrip(st_window,menu);
  69.  
  70.   AddGadget(st_window, &STDoneGad,-1L);
  71.   AddGadget(st_window, &delay_prop,-1L);
  72.   RefreshStatus();
  73. }
  74.  
  75. RefreshStatus()
  76. {
  77.   ULONG   AvailMem();
  78.   extern  USHORT printon, capture;
  79.  
  80.   if(st_window == NULL) return;
  81.   strcpy(cap_status,(*capt_status == ' ') ? (UBYTE *)"OFF" : capt_status);
  82.   strcpy(cap_file,capture ? install.cap_name : (UBYTE *)"No capture file");
  83.  
  84.   sprintf(mem_chip,"CHIP: %4ld K",AvailMem(MEMF_CHIP) >> 10);
  85.   sprintf(mem_fast,"FAST: %4ld K",AvailMem(MEMF_FAST) >> 10);
  86.   sprintf(prt_status,"%s", printon ? "ON" : "OFF");
  87.  
  88.   SetAPen(st_window->RPort,1L);
  89.   RectFill(st_window->RPort,0L,10L,
  90.          (long)st_window->Width,(long)st_window->Height);
  91.  
  92.   PrintIText(st_window->RPort,&STchip,0L,0L);
  93.   RefreshGadgets(st_window->FirstGadget,st_window,0L);
  94.   WindowToFront(st_window);
  95. }
  96.  
  97. /* Try to find the default directory the user has.
  98. */
  99. Defdir()
  100. {
  101.    return;  /* not used */
  102. }
  103.  
  104. /*************************************************
  105. *  function to get a string
  106. *************************************************/
  107. getstring(gtxt,text,name,len)
  108. UBYTE *text,*name,*gtxt;
  109. int len;
  110. {
  111.    void emits_tx();
  112.  
  113.    if(req_window)    /* if already open, return error */
  114.       return FALSE;
  115.    string.BufferPos = 0;
  116.    string.Buffer = name;            /* pass buffer pointer to gadget */
  117.    string.MaxChars = (SHORT)len -1; /* length of buffer */
  118.    Reqtext.IText = text;            /* pass text to gadgets */
  119.    oktext.IText  = gtxt;
  120.  
  121.    if(( req_window = MyOpenWindow(&RQWindow,(ULONG)GADGETUP) ) == NULL)
  122.    {
  123.      emits_tx("Can't open requester window\n");
  124.      return FALSE;
  125.    }
  126.  
  127.    SetAPen(req_window->RPort,1L);
  128.  
  129.    RectFill(req_window->RPort,0L,10L,
  130.             (long)req_window->Width,(long)req_window->Height);
  131.  
  132.    DrawBorder(req_window->RPort,&Reqborder,0L,0L);
  133.    AddGadget(req_window, &Strgadget,    -1L);
  134.    AddGadget(req_window, &OKgadget,     -1L);
  135.    AddGadget(req_window, &Cancelgadget, -1L);
  136.  
  137.    WaitTOF(); WaitTOF();
  138.    RefreshGadgets(&Strgadget,req_window,0L);
  139.    if( dos_version == DOS12 )
  140.       return ActivateGadget(&Strgadget,req_window,0L);
  141.    else return TRUE;
  142. }
  143.  
  144. getfile(text,name)
  145. UBYTE *text,*name;
  146. {
  147.    return(get_fname(rx_window,commscreen,text,name,install.def_dir) != 0);
  148. }
  149.  
  150. /********************************
  151.  do_crc() computes CRC using the
  152.  CCITT polynomial.
  153. ********************************/
  154.  
  155. do_crc( val )
  156. UBYTE val;
  157. {
  158.    USHORT shifter,flag;
  159.  
  160.    if(crcflag)
  161.      for( shifter = 0x80 ; shifter ; shifter >>= 1 )
  162.        {
  163.          flag = (crc & 0x8000);
  164.          crc <<= 1;
  165.          crc |= ((shifter & val) ? 1 : 0);
  166.          if( flag )
  167.             crc ^= 0x1021;
  168.        }
  169.    else
  170.      checksum += val;
  171. }
  172.  
  173. /******************************
  174.  reports statistics of a file
  175. ******************************/
  176.  
  177. void report(file)
  178. char *file;
  179. {
  180.    extern struct FileLock *Lock();
  181.    ULONG  size, sectors, seconds;
  182.    struct FileInfoBlock *FBlock;
  183.    struct FileLock      *FLock;
  184.    void   emits_rx();
  185.  
  186.    if((FLock = Lock(file,ACCESS_READ)) == NULL)
  187.       return;
  188.  
  189.    if((FBlock = (struct FileInfoBlock *)
  190.                 AllocMem((long)sizeof(struct FileInfoBlock),MEMF_CHIP)) != NULL)
  191.      if( Examine(FLock,FBlock) )
  192.         {
  193.            size = FBlock->fib_Size;
  194.            if(size != 0)
  195.              {
  196.                sectors = size / SECSIZ +1L;
  197.                sprintf(sbuff,"\nFile contains %ld bytes, %ld blocks.\n",
  198.                   size, sectors);
  199.                emits_rx(sbuff);
  200.                seconds = sectors * 133L * 11L / (long)baud;
  201.                sprintf(sbuff,"Approx. %ld minutes %ld seconds for transfer at %d bps.\n",
  202.                   seconds / 60L, seconds % 60L, baud);
  203.                emits_rx(sbuff);
  204.              }
  205.            FreeMem(FBlock, (long)sizeof(struct FileInfoBlock));
  206.         }
  207.    UnLock(FLock);
  208.    return;
  209. }
  210.  
  211. file_exists(name)
  212. char *name;
  213. {
  214.    extern struct FileLock *Lock();
  215.    struct FileLock      *FLock;
  216.  
  217.    if((FLock = Lock(name,ACCESS_READ)) == NULL)
  218.       return FALSE;
  219.    UnLock(FLock);
  220.    return TRUE;
  221. }
  222.  
  223. use_file(name)
  224. char *name;
  225. {
  226.   return( AutoRequest(rx_window,&File_exists,&Continue,&Cancel,
  227.       0L,0L,264L,55L) );
  228. }
  229.  
  230. no_file()
  231. {
  232.   return( AutoRequest(rx_window,&No_File,&Continue,&Continue,
  233.       0L,0L,264L,55L) );
  234. }
  235.  
  236. app_del()
  237. {
  238.   return( AutoRequest(rx_window,&What_to_do,&Append,&Delete,
  239.       0L,0L,264L,55L) );
  240. }
  241.  
  242.  
  243. /*************************************************
  244. *  functions to output ascii chars to window
  245. *************************************************/
  246. void  emit_tx(c)
  247. UBYTE c;
  248. {
  249.    _emit(tx_con,c);
  250. }
  251.  
  252. void  emit_rx(c)
  253. UBYTE c;
  254. {
  255.    _emit(rx_con,c);
  256. }
  257.  
  258. void  emit_st(c)
  259. UBYTE c;
  260. {
  261.    _emit(st_con,c);
  262. }
  263.  
  264. void  emit_vw(c)
  265. UBYTE c;
  266. {
  267.    if(debug && SHOWPROT)
  268.      return;
  269.    _emit(vw_con,c);
  270. }
  271.  
  272. void  emits_tx(str)
  273. UBYTE  *str;
  274. {
  275.    _emits(tx_con,str);
  276. }
  277.  
  278. void  emits_rx(str)
  279. UBYTE  *str;
  280. {
  281.    _emits(rx_con,str);
  282. }
  283.  
  284. void  emits_st(str)
  285. UBYTE  *str;
  286. {
  287.    _emits(st_con,str);
  288. }
  289.  
  290. void  emits_vw(str)
  291. UBYTE  *str;
  292. {
  293.  if(vw_window)
  294.    _emits(vw_con,str);
  295. }
  296.  
  297. void _emit(con,c)
  298. struct IOStdReq *con;
  299. UBYTE c;
  300. {
  301.    con->io_Command = CMD_WRITE;
  302.    con->io_Data = (APTR)&c;
  303.    con->io_Length = 1;
  304.    DoIO(con);
  305. }
  306.  
  307. void _emits(con,string)
  308. UBYTE *string;
  309. struct IOStdReq *con;
  310. {
  311.    con->io_Command = CMD_WRITE;
  312.    con->io_Length = -1;
  313.  
  314.    con->io_Data = (APTR)string;
  315.    DoIO(con);
  316. }
  317.  
  318. status_line(xpos,str)   /* xpos not used */
  319. UBYTE *str;
  320. int xpos;
  321. {
  322.   UBYTE temp[ 80 ];
  323.  
  324.   strncpy(temp,str,78);
  325.   temp[79] = '\r';
  326.   sprintf(sbuff,"\033[0;%dH",xpos); emits_st(sbuff);
  327.   emits_st(temp);
  328. }
  329.  
  330. clear_status_line()
  331. {
  332.   emits_st("\2330;33;40m\2330 p\014");
  333. }
  334.  
  335. emit_rx_protocol(ch)
  336. UBYTE ch;
  337. {
  338.    if(vw_window && (debug & SHOWPROT))
  339.    {
  340.      sprintf(sbuff,"\2330;33;40m%02.2x \2330;31;40m",ch);
  341.      emits_vw(sbuff);
  342.    }
  343. }
  344.  
  345. emit_tx_protocol(ch)
  346. UBYTE ch;
  347. {
  348.    if(vw_window && (debug & SHOWPROT))
  349.    {
  350.      sprintf(sbuff,"%02.2x ",ch);
  351.      emits_vw(sbuff);
  352.    }
  353. }
  354.  
  355. /*
  356.    split screen into a large receive window and a small transmit window
  357. */
  358. Split_Window()
  359. {
  360.   extern void SetWindowTitles(), SizeWindow(), MoveWindow();
  361.  
  362.   AllocMem(16500000L,0L);                /* force memory compaction */
  363.  
  364.   if(( rx_window = (struct Window *)
  365.         MyOpenWindow(&RXWindow,(ULONG) GADGETUP) ) == NULL)
  366.    {
  367.      emits_tx("Can't open receive window\n");
  368.      rx_window = tx_window;
  369.      return FALSE;
  370.    }
  371.  
  372.   if(this_process)
  373.     this_process->pr_WindowPtr = (APTR)rx_window;
  374.  
  375.   SetMenuStrip(rx_window,menu);
  376.   add_window_gadgets(rx_window);
  377.   emit_tx(12);
  378.   emit_rx(12);
  379.  
  380.   SizeWindow(rx_window,
  381.             (long)(RXWindow.Width     - rx_window->Width),
  382.             (long)(RXWindow.MinHeight - rx_window->Height) );
  383.   MoveWindow(rx_window,
  384.           (long)(0                 - rx_window->LeftEdge),
  385.           (long)(RXWindow.TopEdge  - rx_window->TopEdge));
  386.  
  387.   SizeWindow(tx_window,
  388.              (long)(TXWindow.Width     - tx_window->Width),
  389.              (long)(TXWindow.MinHeight - tx_window->Height) );
  390.   MoveWindow(tx_window,
  391.           (long)(0                  - tx_window->LeftEdge),
  392.           (long)(RXWindow.MinHeight - tx_window->TopEdge));
  393.  
  394.   Delay(20L);              /* wait till the screen stops */
  395.  
  396.   reopen_tx_con();
  397.   reopen_rx_con();
  398.  
  399.   emits_rx("\233t");
  400.   emits_tx("\233t\2332y");
  401.  
  402.   Move(tx_window->RPort,0L,0L);          /* position to top of window */
  403.   Draw(tx_window->RPort,(long)WIDTH,0L); /* give us a reference line */
  404.  
  405.   return TRUE;
  406. }
  407.  
  408. /*
  409.    revert back to 1 single window, full screen size
  410. */
  411. void Single_Window()
  412. {
  413.  
  414.   if(split_screen)                    /* 2 windows were open */
  415.   {
  416.     ClearMenuStrip(rx_window);
  417.     MyCloseWindow(rx_window);         /* close the RX window */
  418.   }
  419. /* position the TX window to top of screen and make it full screen size */
  420.   MoveWindow(tx_window,
  421.          (long)(0                - tx_window->LeftEdge),
  422.          (long)(RXWindow.TopEdge - tx_window->TopEdge) );
  423.  
  424.   SizeWindow(tx_window,
  425.        (long)(TXWindow.Width  - tx_window->Width),
  426.        (long)(TXWindow.Height - tx_window->Height) );
  427.   rx_window = tx_window;
  428.  
  429.   Delay(20L);
  430.   reopen_tx_con();
  431.   reopen_rx_con();
  432.  
  433.   emits_tx("\233t");
  434.   emits_rx("\233t");
  435.  
  436.   split_screen = close_window = FALSE;
  437.   emit_tx(12);    /* then clear it */
  438.  
  439.   if(this_process)
  440.     this_process->pr_WindowPtr = (APTR)rx_window;
  441.  
  442. }
  443.  
  444. /*
  445.    Open a window.  IDCMP port is fixed to send messages to 1 port, the
  446.    tx_window port.
  447. */
  448. struct Window *MyOpenWindow(which,flags)
  449. struct NewWindow *which;
  450. ULONG flags;
  451. {
  452.    struct Window *win;
  453.    if(( win = OpenWindow(which) ) == NULL)
  454.      return NULL;
  455.  
  456.    win->UserPort = tx_window->UserPort;
  457.    ModifyIDCMP(win,flags | RAWKEY | MENUPICK);
  458.    return win;
  459. }
  460.  
  461. /*
  462.    Close window.  Set IDCMP message port to NULL so Intuition will handle
  463.    the close properly.
  464. */
  465. MyCloseWindow(which)
  466. struct Window *which;
  467. {
  468.    which->UserPort = NULL;
  469.    CloseWindow(which);
  470.    return NULL;
  471. }
  472.  
  473.  
  474. add_window_gadgets(win)
  475. struct Window *win;
  476. {
  477.    if(win == tx_window)
  478.    {
  479.       AddGadget(tx_window,&tx_window_close,-1L);
  480.       AddGadget(tx_window,&tx_window_front,-1L);
  481.       AddGadget(tx_window,&tx_window_back ,-1L);
  482.    }   
  483.    else if(win == rx_window)
  484.    {
  485.       AddGadget(rx_window,&rx_window_close,-1L);
  486.       AddGadget(rx_window,&rx_window_front,-1L);
  487.       AddGadget(rx_window,&rx_window_back ,-1L);
  488.    }
  489. }
  490.  
  491. open_console(window,request)
  492. struct IOStdReq *request;
  493. struct Window   *window;
  494. {
  495.    request->io_Data = (APTR)window;
  496.    request->io_Length = sizeof(*window);
  497.    return(OpenDevice("console.device",0L,request,0L));
  498. }
  499.  
  500. close_console(request)
  501. struct IOStdReq *request;
  502. {
  503.    CloseDevice(request);
  504. }
  505.  
  506. reopen_tx_con()
  507. {
  508.   close_console(tx_con);
  509.   DeleteStdIO(tx_con);
  510.   tx_con = CreateStdIO(tx_con_mp);
  511.   open_console(tx_window,tx_con);
  512. }
  513. reopen_rx_con()
  514. {
  515.   close_console(rx_con);
  516.   DeleteStdIO(rx_con);
  517.   rx_con = CreateStdIO(rx_con_mp);
  518.   open_console(rx_window,rx_con);
  519. }
  520.  
  521.  
  522. /* test any entered filename for .arc or .ARC */
  523. test_4_arc(fname)
  524. char *fname;
  525. {
  526.    char *p;
  527.    p = &fname[ strlen(fname)-4 ];
  528.    if( (strcmp(p,".ARC") == 0) || (strcmp(p,".arc") == 0) )
  529.    {
  530.       if(chopflg)
  531.          emits_rx("CHOP mode disabled for this ARC file\n");
  532.       return TRUE;
  533.    }
  534.    return FALSE;
  535. }
  536.  
  537.  
  538.